package com.ler.yrmall.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ler.yrmall.basic.Result;
import com.ler.yrmall.product.entity.AttrAttrGroupRelationEntity;
import com.ler.yrmall.product.entity.AttrGroupEntity;
import com.ler.yrmall.product.form.AttrGroupForm;
import com.ler.yrmall.product.service.AttrAttrgroupRelationService;
import com.ler.yrmall.product.service.AttrGroupService;
import com.ler.yrmall.product.service.CategoryService;
import com.ler.yrmall.product.vo.AttrGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author Leron
 */
@RestController
@RequestMapping("/attrGroup")
public class AttrGroupController {

    @Resource
    private AttrGroupService attrGroupService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 分类下的所有分组
     */
    @RequestMapping("/find")
    public Result<?> find(AttrGroupForm request) {
        return Result.ok(attrGroupService.find(request));
    }

    @RequestMapping("/list")
    public Result<?> listByCategoryId(AttrGroupForm request) {
        return Result.ok(attrGroupService.list(request));
    }

    /**
     * 关联属性列表
     * @param groupId
     * @return
     */
    @GetMapping("/relationList/{groupId}")
    public Result<?> attrRelation(@PathVariable Long groupId){
        return Result.ok(attrGroupService.getAttrRelation(groupId));
    }

    /**
     * 未关联的属性列表
     * @param request
     * @return
     */
    @GetMapping("/uncorrelatedAttr/relation")
    public Result<?> noAttrRelation(AttrGroupForm request){
        return Result.ok(attrGroupService.getNoAttrRelation(request));
    }

    /**
     * 信息
     */
    @RequestMapping("/getById")
    public Result<?> getById(long id) {
        AttrGroupEntity attrGroup = attrGroupService.getById(id);
        Long categoryId = attrGroup.getCategoryId();
        AttrGroupVO target = new AttrGroupVO();
        BeanUtils.copyProperties(attrGroup,target);
        target.setCategoryPath(Arrays.asList(categoryService.getCategoryPath(categoryId)));
        return Result.ok(target);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result<?> save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result<?> update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);
        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result<?> delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return Result.ok();
    }

    @PostMapping("/attr/relation")
    public Result<?> addAttrRelation(@RequestBody List<AttrAttrGroupRelationEntity> params){
        List<AttrAttrGroupRelationEntity> list = new ArrayList<>();
        params.forEach(item -> {
            AttrAttrGroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrGroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrId(item.getAttrId());
            attrAttrgroupRelationEntity.setAttrGroupId(item.getAttrGroupId());
            list.add(attrAttrgroupRelationEntity);
        });
        attrAttrgroupRelationService.saveBatch(list);
        return Result.ok();
    }


    @PostMapping("/attr/relation/delete")
    public Result<?> deleteRelation(@RequestBody List<AttrAttrGroupRelationEntity> params){
        params.forEach(item -> attrAttrgroupRelationService.remove(Wrappers.<AttrAttrGroupRelationEntity>lambdaQuery()
                .eq(AttrAttrGroupRelationEntity::getAttrId,item.getAttrId())
                .eq(AttrAttrGroupRelationEntity::getAttrGroupId,item.getAttrGroupId())));
        return Result.ok();
    }

    @GetMapping("/getGroupWithAttr/{categoryId}")
    public Result<?> getAttrGroupWithAttrs(@PathVariable Long categoryId){
        List<AttrGroupVO> result = attrGroupService.getAttrGroupWithAttrs(categoryId);
        return Result.ok(result);
    }
}
